﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using BUS;

namespace FastDeal
{
    public partial class DoanhThuTheoDoanhNghiep : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["DaDangNhap"] == null || (bool)Session["DaDangNhap"] == false)
            {
                Response.Redirect("~/Default.aspx");
            }
            if ((int)Session["LoaiThanhVien"] != 1)
            {
                Response.Redirect("~/Default.aspx");
            }
            if (!IsPostBack)
            {              
                LoadDSDoanhNghiep();
                lbTongThanhTien.Visible = false;
                Label1.Visible = false;
                lbTen.Visible = false;
            }
        }

        TaiKhoan tk = new TaiKhoan();
        TaiKhoan_BUS tkBUS = new TaiKhoan_BUS();
        
        private void LoadDSDoanhNghiep()
        {
            List<TaiKhoan> listdn = new List<TaiKhoan>();
            listdn = tkBUS.LayDanhSachDoanhNghiep();
            ddlDoanhNghiep.DataSource = listdn;
            ddlDoanhNghiep.DataTextField = "UserName";
            ddlDoanhNghiep.DataValueField = "UserID";
            ddlDoanhNghiep.DataBind();
        }

        private void LoadDoanhThu(TaiKhoan tk)
        {
            IQueryable ilist;
            ilist = tkBUS.DoanhThuTheoDoanhNghiep(tk);
            gvDoanhThu.DataSource = ilist;
            gvDoanhThu.DataBind();          
        }

        protected void btnDoanhThu_Click(object sender, EventArgs e)
        {
            tk.UserID = Convert.ToInt32(ddlDoanhNghiep.SelectedValue);
           // LoadDoanhThu(tk);
            IQueryable ilist;
            ilist = tkBUS.DoanhThuTheoDoanhNghiep(tk);
            gvDoanhThu.DataSource = ilist;
            gvDoanhThu.DataBind();          

            // tính tổng tiền
            double tongTien = 0;
            for (int i = 0; i < gvDoanhThu.Rows.Count - 1; i++)
            {
                double tien = Convert.ToDouble(((Label)gvDoanhThu.Rows[i].Cells[1].FindControl("lbTongTien")).Text);
                tongTien += tien;
            }
            lbTen.Text = "Thống Kê Doanh Nghiệp:  " + ddlDoanhNghiep.SelectedItem.ToString();

            lbTen.Visible = true;
            lbTongThanhTien.Text = tongTien.ToString();
            Label1.Visible = true;
            lbTongThanhTien.Visible = true;
            
        }
    }
}